package restyclient

import (
	"crypto/tls"
	"encoding/json"
	"github.com/go-resty/resty/v2"
	"golang.org/x/net/proxy"
	global2 "longmen/server/config/global"
	"longmen/server/internal/services/nami/config"
	"net/http"
	"time"
)

const (
	debugResty = "debug.resty"
)

func autoSetProxy(client *resty.Client) {
	if config.Nami.Resty.ProxyType == "url" {
		client.SetProxy(config.Nami.Resty.ProxyUrl)
	} else if config.Nami.Resty.ProxyType == "socks5" {
		dialer, err := proxy.SOCKS5("tcp", config.Nami.Resty.ProxyUrl, &proxy.Auth{
			User:     config.Nami.Resty.ProxyUser,
			Password: config.Nami.Resty.ProxyPass,
		}, proxy.Direct)
		if err != nil {
			global2.Log.Fatalf("Unable to obtain proxy dialer: %v\n", err)
		}
		transport := &http.Transport{Dial: dialer.Dial}
		client.SetTransport(transport)
	}
}

func GetUrlAndBindRecords(url string, records interface{}) error {
	client := resty.New().SetTimeout(5 * time.Second)
	autoSetProxy(client)
	client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp, err := client.R().Get(config.Nami.Domain + url)
	if err != nil {
		global2.Log.Error("can't get GetUrlAndBindRecords push url: ", err)
		return err
	}
	global2.Log.Info("curr url: ", config.Nami.Domain+url)
	//global.Log.Info(" response: ", resp.String())
	err = json.Unmarshal(resp.Body(), &records)
	if err != nil {
		global2.Log.Error(
			"can't Unmarshal GetUrlAndBindRecords response: ", err,
			"json body: ", resp.String(),
		)
		return err
	}
	if global2.Config.GetInt(debugResty) == 1 {
		global2.Log.Info("抓取数据：", resp.String(), "抓取URL：", url)
	}

	return nil
}

func GetUrlAndBindRecordsResponse(url string, records interface{}) (error, *resty.Response) {
	client := resty.New()
	autoSetProxy(client)
	client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp, err := client.R().Get(config.Nami.Domain + url)
	if err != nil {
		global2.Log.Error("can't get GetUrlAndBindRecords push url: ", err)
		return err, nil
	}
	global2.Log.Info("curr url: ", config.Nami.Domain+url)
	//global.Log.Info(" response: ", resp.String())
	err = json.Unmarshal(resp.Body(), &records)
	if err != nil {
		global2.Log.Error(
			"can't Unmarshal GetUrlAndBindRecords response : ", err,
			"json body: ", resp.String(),
		)
		return err, nil
	}
	if global2.Config.GetInt(debugResty) == 1 {
		global2.Log.Info("抓取数据：", resp.String(), "抓取URL：", url)
	}
	return nil, resp
}

func GetVideoUrlAndBindRecords(url string, records interface{}) error {
	client := resty.New()
	autoSetProxy(client)
	client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp, err := client.R().Get(config.Nami.VideoDomain + url)
	if err != nil {
		global2.Log.Error("can't get GetUrlAndBindRecords push url: ", err)
		return err
	}
	global2.Log.Info("curr url: ", config.Nami.VideoDomain+url)
	//global.Log.Info(" response: ", resp.String())
	err = json.Unmarshal(resp.Body(), &records)
	if err != nil {
		global2.Log.Error(
			"can't Unmarshal GetUrlAndBindRecords response : ", err,
			"json body: ", resp.String(),
		)
		return err
	}

	if global2.Config.GetInt(debugResty) == 1 {
		global2.Log.Info("抓取数据：", resp.String(), "抓取URL：", url)
	}

	return nil
}

func ESportGetUrlAndBindRecords(url string, records interface{}) error {
	client := resty.New()
	autoSetProxy(client)
	client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp, err := client.R().Get(config.Nami.ESportDomain + url)
	if err != nil {
		global2.Log.Error("can't get GetUrlAndBindRecords push url: ", err)
		return err
	}
	global2.Log.Info("curr url: ", config.Nami.ESportDomain+url)
	//global.Log.Info(" response: ", resp.String())
	err = json.Unmarshal(resp.Body(), &records)
	if err != nil {
		global2.Log.Error(
			"can't Unmarshal GetUrlAndBindRecords response: ", err,
			"json body: ", resp.String(),
		)
		return err
	}

	if global2.Config.GetInt(debugResty) == 1 {
		global2.Log.Info("抓取数据：", resp.String(), "抓取URL：", url)
	}

	return nil
}
